import * as IMApi from '@src/api/ImApi.js';
import { Message } from 'element-ui'

/**
 * TextParams 构造文本类型的消息参数
 */
class TextParams {
  constructor(data = {}) {
    this.chatType = data.chatType
    this.chatUuid = data.chatUuid
    this.messageType = data.messageType
    this.messageContent = {
      msg: data.messageContent.msg,
      type: data.messageType
    }
  }
}

/**
 * TextParams 构造图片类型的消息参数
 */
class ImageParams {
  constructor(data = {}) {
    this.chatType = data.chatType
    this.chatUuid = data.chatUuid
    this.messageType = data.messageType
    this.messageContent = {
      fileName: data.messageContent.fileName,
      fileSize: data.messageContent.fileSize,
      url: data.messageContent.url,
      type: data.messageType,
      fileId:data.messageContent.fileId
    }
  }
}

/**
 * TextParams 构造文件类型的消息参数
 */
class FileParams {
  constructor(data = {}) {
    this.chatType = data.chatType
    this.chatUuid = data.chatUuid
    this.messageType = data.messageType
    this.messageContent = {
      fileName: data.messageContent.fileName,
      fileSize: data.messageContent.fileSize,
      url: data.messageContent.url,
      type: data.messageType,
      fileId:data.messageContent.fileId
    }
  }
}

/**
 * EventParams 构造事件业务类型的消息参数
 */
class EventParams {
  constructor(data = {}) {
    this.chatType = data.chatType
    this.chatUuid = data.chatUuid
    this.messageType = data.messageType
    this.messageContent = {
      eventTypeId: data.messageContent.eventTypeId,
      eventTypeName: data.messageContent.eventTypeName,
      eventNo: data.messageContent.eventNo,
      eventId:data.messageContent.eventId,
      customerId:data.messageContent.customerId,
      customerName:data.messageContent.customerName,
      type: data.messageType,
      pageUrl:data.messageContent.pageUrl
    }
  }
}

/**
 * TaskParams 构造工单业务类型的消息参数
 */
class TaskParams {
  constructor(data = {}) {
    this.chatType = data.chatType
    this.chatUuid = data.chatUuid
    this.messageType = data.messageType
    this.messageContent = {
      taskTypeId: data.messageContent.taskTypeId,
      taskTypeName: data.messageContent.taskTypeName,
      taskNo: data.messageContent.taskNo,
      taskId: data.messageContent.taskId,
      customerId:data.messageContent.customerId,
      customerName:data.messageContent.customerName,
      type: data.messageType,
      pageUrl:data.messageContent.pageUrl
    }
  }
}

/**
 * TaskParams 构造工单业务类型的消息参数
 */
class CustomerParams {
  constructor(data = {}) {
    this.chatType = data.chatType
    this.chatUuid = data.chatUuid
    this.messageType = data.messageType
    this.messageContent = {
      customerId:data.messageContent.customerId,
      customerName:data.messageContent.customerName,
      type: data.messageType,
      pageUrl:data.messageContent.pageUrl,
      linkmanId:data.messageContent.linkmanId,
      linkmanName:data.messageContent.linkmanName,
      linkmanPhone:data.messageContent.linkmanPhone,
    }
  }
}

/**
 * 发送基础消息消息
 * @param {} obj
 */
export async function sendSingleMessage(obj = {}) {
  try {
    let messageType = obj.messageType
    let params = {}
    if (messageType == 'text') params = new TextParams(obj)
    if (messageType == 'image') params = new ImageParams(obj)
    if (messageType == 'file') params = new FileParams(obj)
    if (messageType == 'paragraph') params = obj
    const { code, data, message } = await IMApi.sendSingleMessage(params)
    if (code == 0) {
      data.rightDisplay = true
      return Promise.resolve(data)
    }
    Message.error(message)
    return Promise.reject(message)
  } catch (err) {
    console.error(err, 'sendSingleMessage请求失败')
    return Promise.reject('sendSingleMessage请求失败')
  }
}

/**
 * 发送业务卡片消息
 * @param {*} obj 
 * @returns 
 */
export async function sendBizMessage(obj = {}) {
  try {
    let messageType = obj.messageType
    let params = {}
    if (messageType == 'event') params = new EventParams(obj)
    if (messageType == 'task') params = new TaskParams(obj)
    if (messageType == 'customer') params = new CustomerParams(obj)
    const { code, data, message } = await IMApi.sendBizMessage(params)
    if (code == 0) {
      data.rightDisplay = true
      return Promise.resolve(data)
    }
    Message.error(message)
    return Promise.reject(message)
  } catch (err) {
    console.error(err, 'sendBizMessage请求失败')
    return Promise.reject('sendBizMessage请求失败')
  }
}